import {action, observable} from 'mobx';
import {WeaForm, WeaTableNew} from "comsMobx";
import * as API_WFREPORT from '../../apis/efficiencyReport';

const {TableStore} = WeaTableNew;

export class RequestListStore {

	@observable tableStore = new TableStore();
	@observable loading = false;
	dataKey = "";

	@observable form = new WeaForm();
	@observable showSearchAd = false;
	@observable condition = [];
	
	@observable reportParams = {};
    @observable selectTabKey =  1;

	constructor() {
		this.setShowSearchAd = this.setShowSearchAd.bind(this);
		this.setFormFields = this.setFormFields.bind(this);
		this.clearFormFields = this.clearFormFields.bind(this);
		this.updateReportParams  = this.updateReportParams.bind(this);
		this.loadCondition = this.loadCondition.bind(this);
		this.doSearch = this.doSearch.bind(this);
		this.changeTab = this.changeTab.bind(this);
	}
	
	loadCondition() {
		this.loading = true;
		API_WFREPORT.getQueryCondition(this.reportParams).then((data) => {
			this.loading = false;
			let condition = data.condition;
			this.condition = condition;
			this.form.initFormFields(condition);
		});
	}

	doSearch(params = {}) {
		//常用查询条件值
		const searchParamsCommon = this.form.getFormParams();
		//拼接查询请求参数
		let queryParams = { ...this.reportParams,...searchParamsCommon};
		this.loading = true;
		API_WFREPORT.getRequestList(queryParams).then((data) => {
            this.tableStore.getDatas(data.sessionkey, params.current || 1);
            this.dataKey = data.sessionkey;
            this.loading = false;
		});
	}

	setShowSearchAd(bool) {
		this.showSearchAd = bool;
	}

    setFormFields(value){
        this.form.updateFields(value, true);    //true代表完全覆盖方式更新条件值
    }

	clearFormFields() {
		this.form.reset(); //清除查询条件值
	}
	
	updateReportParams(params){
		this.reportParams  = {...this.reportParams,...params};
	}

    changeTab(selectTabKey){
        this.selectTabKey = selectTabKey;
        let flowStatus = '';
        if(selectTabKey == 2) flowStatus = 0;
        if(selectTabKey == 3) flowStatus = 1;
        this.updateReportParams({flowStatus});
    }

}